﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Purchase : System.Web.UI.Page
{
    private object fileDataStream;

    protected void Page_Load(object sender, EventArgs e)
    {
        UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection sqlcon = new SqlConnection("Data Source=LocalHost;Integrated Security=SSPI;Initial Catalog=keshe2;");
            sqlcon.Open();
            string fileAttention = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);//扩展名
            string fileName = System.IO.Path.GetFileName(FileUpload1.FileName.ToString());
            // String photoFileName = "~/image/" + fileName + fileAttention;
            string photoFileName = "~/image/" + fileName;

            string InsertSql = "insert into 商品 (商品编号,商品名称,数量,图片,价格,商品类型) values(" +
                 "'" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + photoFileName + "','" + TextBox4.Text + "','" + RadioButtonList1.SelectedValue + "')";

            SqlCommand com = new SqlCommand(InsertSql, sqlcon);
            com.ExecuteNonQuery();
            SqlDataAdapter ada = new SqlDataAdapter("select * from 商品", sqlcon);
            DataSet ds = new DataSet();
            ada.Fill(ds, "商品 ");
            //GridView1.DataSource = ds;
            GridView1.DataBind();
            sqlcon.Close();
            //Response.Write("alert('添加成功!')");
            zhushi.Text = "添加成功";
        }
        catch
        {
            //Response.Write("alert('添加失败!')");
            zhushi.Text = "添加失败";
        }



    }

    protected void btnValue_Click(object sender, EventArgs e)
    {
        SqlConnection sqlcon;
        SqlCommand sqlcom;

        String strConnection = "Server=.;Database=keshe2;Integrated Security=SSPI";
        SqlConnection con = new SqlConnection(strConnection);
        con.Open();
        //String sql = "SELECT 商品编号,图片,商品名称,价格 FROM 商品";
        String sql = "SELECT 商品.商品编号,商品.商品名称,商品.数量,商品.图片,商品.价格,商品类型.名称 FROM 商品,商品类型 WHERE 商品.商品类型=商品类型.编号";
        sqlcon = new SqlConnection(strConnection);
        sqlcon.Open();
        sqlcom = new SqlCommand(sql, sqlcon);
        SqlDataReader Dr = sqlcom.ExecuteReader();
        int a = 0;
        try
        {
            if (txtValue.Text.Trim().Length != 0)
            {
                while (Dr.Read())
                {
                    if (Dr[DropDownList1.SelectedValue].ToString().Contains(txtValue.Text))
                    {
                        GridView1.DataSourceID = "SqlDataSource1";
                        string sql1 = "SELECT 商品.商品编号,商品.商品名称,商品.数量,商品.图片,商品.价格,商品类型.名称 FROM 商品,商品类型 WHERE 商品.商品类型=商品类型.编号 AND " + DropDownList1.SelectedValue + " LIKE '%" + txtValue.Text + "%'";
                        SqlDataSource1.SelectCommand = sql1;
                        SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                        a = 1;
                        break;
                    }

                }
                if (a == 0)
                {
                    GridView1_empty();
                }
            }
        }
        catch (SqlException ex)
        {
            Response.Write(ex.Message.ToString());
        }
        Dr.Close();
        sqlcon.Close();
    }


    protected void GridView1_empty()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("商品编号");
        dt.Columns.Add("商品名称");
        dt.Columns.Add("数量");
        dt.Columns.Add("价格");
        dt.Columns.Add("图片");
        dt.Columns.Add("名称");
        dt.Columns.Add("商品图片");
        if (dt.Rows.Count == 0)
        {
            dt.Rows.Add(dt.NewRow());
            GridView1.DataSourceID = "";
            GridView1.DataSource = dt;
            GridView1.DataBind();
            int columnCount = dt.Columns.Count;
            GridView1.Rows[0].Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = columnCount;
            GridView1.Rows[0].Cells[0].Text = "暂无该商品记录！";
            GridView1.Rows[0].Cells[0].Style.Add("text-align", "center");
        }

    }


    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.ToString());

    }
}